/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.ast;


/** an AST node representing a function */
public class Function extends InvocablePara {

    /**
     * index of returnDecl in child list
     */
    private static final int RETURNDECL_INDEX = 3;

    /** the expression represented by this function */
    private Expr _expr;

    public Function(Location location_, QualifiedName name_, 
                    Expr expr_, ArgList argList_, MultiplicityExpr returnExpr_) {
        super(location_, name_, _expr2formula(expr_, name_), argList_);
        this._expr = expr_;
        addChild(_makeReturnDecl(name_, returnExpr_));
    }


    public Function(QualifiedName name_, Expr expr_,
                    ArgList argList_, MultiplicityExpr returnExpr_) {
        this(Location.UNKNOWN, name_, expr_, argList_, returnExpr_);
    }

    //GET AND SET METHODS

    /**
     * Returns this function's return declaration
     */
    public Decl getReturnDecl() {
        return (Decl)childAt(RETURNDECL_INDEX);
    }

    /**
     * Returns this function's expression
     */
    public Expr getExpression() {
        return ((ElemFormula)getFormula().getFormulas().childAt(0)).getRight();
    }

    /**
     * accept an AST visitor
     * @param visitor_ the visitor
     */
    protected void acceptVisitor(ASTVisitor visitor_) {
        visitor_.visit(this);
    }

    /**
     * accept a returning AST visitor
     * @param visitor_ the visitor
     * @return the Object returned by the visitor
     */
    protected Object acceptReturnVisitor(ASTReturnVisitor visitor_) {
        return visitor_.visit(this);
    }

    public String nodeString() {
        StringBuffer ret = new StringBuffer("fun ");
        // check for empty receiver and result decl
        Decl receiverDecl = getArgList().getReceiverDecl();
        if (!(receiverDecl.isEmpty())) {
            ret.append(receiverDecl.getExpr().nodeString() + ".");
        }
        ret.append(getName().nodeString() + " " +
		   getArgList().getDecls().childrenStr("", ", ", "") + " )");
        ret.append(" : " + getReturnDecl().getExpr().nodeString());
        ret.append(" { " + getExpression().nodeString() + " } ");
        return ret.toString();
    }

    public String toString() {
        return nodeString();
    }

     /**
      * Converts a single expression expr into the singleton formula sequence that
      * contains the formula "result = expr"
      */
    private static FormulaSeq _expr2formula(Expr expr, QualifiedName name) {
      Location exprLocation = expr.getLocation();

      // Construct the formula "functionName = expr"
      Variable returnVar = new Variable(name.getId());
      VariableExpr returnVarExpr = new VariableExpr(returnVar);
      CompOp equalOp = CompOp.EQUALS;
      Formula exprFormula = new ElemFormula(exprLocation, returnVarExpr, equalOp, expr);

      // Create the singleton formula sequence from this formula
      Formulas formulas = new Formulas(exprLocation);
      formulas.addFormula(exprFormula);
      return new FormulaSeq(exprLocation, formulas);
    }

     /**
      * Creates a function return declaration out of a name and return expr
      */
    private static Decl _makeReturnDecl(QualifiedName name, MultiplicityExpr returnExpr) {
        Variables vars = new Variables();
        Variable returnVar  = new Variable(name.getId());
        vars.addVariable(returnVar);
        return new Decl(returnExpr.getLocation(),
                        new Qualifiers(),
                        vars,
			returnExpr);
    }
}
















